/**
 * 实现钟表的绘制
 * 创建者：Carburn Ashroom
 * 2024.2.28
 */

#include "watch.h"

namespace Watch {

    void Watch::draw_back(QPixmap* dest) const
    {
        QPainter pa {dest};
        QPen ori_pen {pa.pen()};
        QPen border {bder.second};
        double border_width {bder.first*radius};
        border.setWidthF(border_width);
        pa.setPen(border);
        pa.drawEllipse(center, radius, radius);
        pa.translate(center);
        pa.setPen(ori_pen);
        pa.setFont(ft);
        for (int i{}; i!=12; ) {
            pa.rotate(360/12);
            int text_loc {static_cast<int>(-radius+border_width/2)};
            pa.drawLine(0, text_loc, 0, -radius-border_width/2);
            pa.drawText(QRect{-20,text_loc,40,40}, Qt::AlignHCenter, QString{}.setNum(++i));
        }
    }
    
    void Watch::draw(QPixmap* dest) const
    {
        draw_back(dest);
        draw_hand(dest);
    }
    
    void Watch::draw_hand(QPixmap* dest) const
    {
        QTime tm {QTime::currentTime()};
        double sec_angle {360/60*(tm.second()+tm.msec()/1000.)};
        double min_angle {360/60*tm.minute()+sec_angle/60};
        double hour_angle {360/12*(tm.hour()%12)+min_angle/12};
        draw_hand(dest, 0, hour_angle);
        draw_hand(dest, 1, min_angle);
        draw_hand(dest, 2, sec_angle);
    }
    
    void Watch::draw_hand(QPixmap* dest, int index, double angle) const
    {
        QPainter pa {dest};
        QPen pen {wch_clr[index]};
        pen.setWidth(wch_thick);
        pa.setPen(pen);
        pa.translate(center);
        pa.rotate(angle);
        pa.drawLine(0, -wch_lth[index]*radius, 0, 0);
    }
    
}
